package com.dr.controller.index;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dr.entity.Address;
import com.dr.entity.User;
import com.dr.service.AddressService;

@Controller
@RequestMapping("/index/")
public class AddressWebController {
	@Autowired
	private AddressService addressService;

	@RequestMapping("/findAddress.do")
	public String findAddress(String idStr, HttpSession session, Model model) {
		// 获取当前登录的用户
		User user = (User) session.getAttribute("loginUser");
		// 查询当前登录用户的登录地址
		List<Address> addresslist = addressService.findAddress(user.getId());
		// 保存数据
		model.addAttribute("ulist",session.getAttribute(user.getName()));
		model.addAttribute("addresslist", addresslist);
		return "address.jsp";
	}
	@RequestMapping("/addAddress.do")
	public String addAddress(Address a, HttpSession session) {
		// 获取当前登录的用户
		User user = (User) session.getAttribute("loginUser");
		a.setUser_id(user.getId());
		addressService.addAddress(a);
		return "findAddress.do";
	}
	@RequestMapping("/updateAddress.do")
	public String updateAddress(Address a, HttpSession session) {
		// 获取当前登录的用户
		User user = (User) session.getAttribute("loginUser");
		a.setUser_id(user.getId());
		addressService.updateAddress(a);
		return "findAddress.do";
	}
}